package com.longsys.framework.server.mongo;

import com.longsys.eventsourcing.core.EventAggregate;
import com.longsys.framework.server.core.EventAggregateRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class MongoEventAggregateRepository implements EventAggregateRepository {

    @Autowired
    private MongoEventRepository mongoEventRepository;

    @Autowired
    private MongoAggregateRepository mongoAggregateRepository;

    @Override
    public void save(EventAggregate eventAggregate) {
        MongoAggregateEntity mongoAggregateEntity = new MongoAggregateEntity(eventAggregate);
        mongoAggregateRepository.save(mongoAggregateEntity);

        ObjectId aggregateObjectId = mongoAggregateEntity.get_id();
        eventAggregate.setAggregateObjectId(aggregateObjectId);
        MongoEventEntity mongoEventEntity = new MongoEventEntity(eventAggregate);
        mongoEventRepository.save(mongoEventEntity);
    }
}
